<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-row>
          <el-button type="success" @click="handleCreate">&emsp;添加&emsp;</el-button>
        </el-row>
      </div>
      <div v-loading="listLoading" class="item">
        <el-table ref="dataList"
                  :data="menuList"
                  border row-key="id" style="width:100%;margin-bottom: 20px;"
        >
          <el-table-column label="序号" type="index" :index="indexMethod" prop="user_code" width="80px"></el-table-column>
          <el-table-column label="边框名称" prop="title"></el-table-column>
          <el-table-column label="边框" min-width="100px" prop="border_img">
            <template slot-scope="scope">
              <el-popover max-width="220px" placement="right" trigger="hover">
                <img :src="scope.row.border_img" alt="" style="max-width: 200px"/>
                <img slot="reference" :src="scope.row.border_img" alt=""
                     style="width: 100px"
                />
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column label="边框标签" min-width="100px" prop="border_tag">
            <template slot-scope="scope">
              <el-popover max-width="220px" placement="right" trigger="hover">
                <img :src="scope.row.border_tag" alt="" style="max-width: 200px"/>
                <img slot="reference" :src="scope.row.border_tag" alt=""
                     style="width: 100px"
                />
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column label="边框类型" prop="type">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.type === 0" type="danger">房间</el-tag>
              <el-tag v-if="scope.row.type === 1" type="success">用户</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="房间ID/用户ID" prop="relation_id"></el-table-column>
          <!-- <el-table-column label="创建时间" prop="relation_id"></el-table-column> -->
          <el-table-column label="操作" width="300px">
            <template slot-scope="scope">
              <el-button icon="el-icon-edit" plain size="mini"
                         type="primary" @click="handleUpdate(scope.row)"
              >编辑
              </el-button>
              <!-- <el-button icon="el-icon-delete" plain
                         size="mini" type="danger" @click="handleDelete(scope.row)"
              >删除
              </el-button> -->
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <cus-pagination :limit.sync="listQuery.perPage" :page.sync="listQuery.page" :total="total"
                        @pagination="getList"
        />
      </div>
    </el-card>
    <!-- 新增编辑弹框 -->
    <el-dialog v-dialogDrag :title="titleMap[dialogStatus]"
               :visible.sync="dialogFormVisible"
               @close="handleCloseDialog('editRulesForm')"
    >
      <el-form ref="editRulesForm" :model="editRulesForm"
               :rules="editRulesFormRules" class="demo-ruleForm" label-width="120px"
      >
        <el-form-item label="边框名称" prop="title">
          <el-input v-model="editRulesForm.title" clearable
                    placeholder="请输入边框名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="边框图片" prop="border_img">
          <el-upload

            :http-request="customizeUpload"
            :on-success="handleSuccess"
            :show-file-list="false"
            action=""
            class="avatar-uploader"
          >
            <img v-if="editRulesForm.border_img"
                 :src="editRulesForm.border_img"
                 class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="标签" prop="border_tag">
          <el-upload

            :http-request="customizeUpload"
            :on-success="handleSuccess1"
            :show-file-list="false"
            action=""
            class="avatar-uploader"
          >
            <img v-if="editRulesForm.border_tag"
                 :src="editRulesForm.border_tag"
                 class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="边框类型" prop="type">
          <el-radio-group v-model="editRulesForm.type">
            <el-radio :label="1">用户</el-radio>
            <el-radio :label="0">房间</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="房间/用户id">
          <el-input v-model="editRulesForm.relation_id" placeholder="请输入房间/用户id，','分割"/>
        </el-form-item>
          
        <el-form-item>
          <el-button @click="dialogFormVisible = false">返回</el-button>
          <el-button type="primary" @click="submitForm('editRulesForm')">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { frameList, frameSave } from '@/api/content/frameList'

export default {
  data() {
    return {
      // confirmBtn: false,
      giveShowDetailPicture: [],
      giveShowPicture: [],
      // token值

      displayOptions: [
        { value: 0, label: '隐藏' },
        { value: 1, label: '显示' }
      ],
      adType: [
        { value: 1, label: '房间' },
        { value: 3, label: '外部链接' },
        { value: 5, label: 'H5' },
        { value: 8, label: '商城上新推荐' },
        { value: 6, label: '无操作' }
      ],
      // 要修改的当前行的idsearchSubmit
      row_id: 0,
      // 表单规则
      editRulesFormRules: {
        title: [
          {
            required: true,
            message: '请输入名称',
            trigger: 'blur',
          }
        ],
        border_img: [{ required: true, message: '请上传边框图片', trigger: 'change' }],
        border_tag: [{ required: true, message: '请上传标签图片', trigger: 'change' }],
      },
      editRulesForm: {
        id: undefined,
        title: undefined,
        type: undefined,
        border_img: undefined,
        border_tag: undefined,
        relation_id: undefined
      },
      dialogStatus: '',
      // dialog的头部信息
      titleMap: {
        update: '编辑',
        create: '新增'
      },
      dialogFormVisible: false,
      deleteDialogVisible: false,
      listLoading: false,
      menuList: [],
      limitList: [],
      listQuery: {
        page: 1,
        perPage: 10,
        keyword: undefined,
        display: undefined
      },
      total: 0,
      // 勾选内容
      multipleSelection: [],
      // 要删除的内容的ids
      dataIds: '',
      options: [],
      skippArr: []
    }
  },
  created() {
    this.getList()
    this.getListAdCategory()
    // this.getBannerSkip()
  },
  methods: {
    indexMethod(index) {
        return this.total - (this.listQuery.page-1) * this.listQuery.perPage - index
    },
    async getListAdCategory() {
      const { data } = await getListAdCategory()
      this.options = data
      this.options.unshift({ id: -1, title: '无', status: 0 })
    },
    getList() {
      this.listLoading = true
      for (let key in this.listQuery) {
          this.listQuery[key] = this.listQuery[key] === '' ? undefined : this.listQuery[key] == 0 ? 0 : this.listQuery[key]
      }
      frameList(this.listQuery).then(response => {
        this.menuList = response.data.data
        this.total = response.data.total
        this.listLoading = false
        // this.confirmBtn = false
      })
    },
    searchSubmit() {
      this.listQuery.page = 1
      this.getList()
    },
    // 点击重置
    resetSubmit() {
      this.listQuery.page = 1
      this.listQuery.perpage = 10
      this.listQuery.display = undefined
      this.listQuery.keyword = undefined
    },
    // 图片上传
    handleRemove(file, fileList) {
      // document.querySelector('.el-upload1 .el-upload').style = 'display:block'
      this.editRulesForm.border_img = ''
      this.giveShowPicture = []
    },
    handleSuccess() {
      this.editRulesForm.border_img = this.ossLastImgUrl
    },
    handleSuccess1() {
      this.editRulesForm.border_tag = this.ossLastImgUrl
    },
    // 点击新增
    handleCreate() {
      this.resetForm('editRulesForm')
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    // 点击编辑
    handleUpdate(row) {
      this.row_id = row.id
      this.editRulesForm.id = row.id
      this.editRulesForm.title = row.title
      this.editRulesForm.border_img = row.border_img
      this.editRulesForm.border_tag = row.border_tag
      this.editRulesForm.relation_id = row.relation_id
      this.editRulesForm.type = row.type
      
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
    },
    handleCloseDialog(formName) {
      if (this.$refs[formName]) {
        this.$refs[formName].clearValidate() // 清除整个表单的校验
      }
    },

    // 重置表单内容
    resetForm(formName) {
      this.$nextTick(() => {
        if (this.$refs[formName] !== undefined) {
          this.$refs[formName].resetFields()
        }
        this.cityList = []
        this.editRulesForm = {
          id: undefined,
          title: undefined,
          type: undefined,
          border_img: undefined,
          border_tag: undefined,
          relation_id: undefined
        }
      })
    },
    submitForm(formName) {

      this.$refs[formName].validate(valid => {
        if (valid) {
          
            frameSave(this.editRulesForm).then(response => {
              if (response.code === 200) {
                this.getList()
                this.dialogFormVisible = false
                this.$notify({
                  type: 'success',
                  message: '操作成功',
                  position: 'bottom-right',
                  showClose: false,
                  duration: 3000
                })
              }
            })
        } else {
          return false
        }
      })
    },
    // 勾选
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 点击单个删除
    handleDelete(row) {
      this.dataIds = { ids: `${row.id}` }
      this.deleteDialogVisible = true
    },
    // 点击批量删除
    handleDeleteAll() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          message: '请先勾选要删除的内容',
          type: 'warning'
        })
        return
      } else if (this.multipleSelection.length !== 0) {
        let ids = ''
        for (let key in this.multipleSelection) {
          ids += `${this.multipleSelection[key].id},`
        }
        ids = ids.substring(0, ids.length - 1)
        this.dataIds = { ids: ids }
      }
      this.deleteDialogVisible = true
    },
    // 确定删除
    confirmDelete() {
      this.deleteDialogVisible = false
      deleteData(this.dataIds).then(response => {
        if (response.code === 200) {
          this.getList()
          this.deleteDialogVisible = false
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          })
        }
      })
    }
  }
}
</script>

<style scoped>
[v-cloak] {
  display : none;
}

.clearfix:before,
.clearfix:after {
  display : table;
  content : "";
}

.clearfix:after {
  clear : both;
}

.box-card {
  width : 100%;
}

.demo-form-inline {
  float : right;
}

/deep/ .el-card__header {
  padding-bottom : 0;
}

::v-deep .el-upload-list__item {
  transition : none !important;
}

::v-deep.avatar-uploader .el-upload {
  border        : 1px dashed #d9d9d9;
  border-radius : 6px;
  cursor        : pointer;
  position      : relative;
  overflow      : hidden;
}

.avatar-uploader .el-upload:hover {
  border-color : #409eff;
}

.avatar-uploader-icon {
  font-size   : 28px;
  color       : #8c939d;
  width       : 100px;
  height      : 100px;
  line-height : 100px;
  text-align  : center;
}

.avatar {
  width   : 100px;
  height  : 100px;
  display : block;
}
</style>
